# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html


import time

class DownloaderPipeline(object):

    def __init__(self):
        self.file = None
        self.startdate = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())

    def close_spider(self, spider):

        if self.file != None:
            print('\nfile close...\n')
            self.file.close()

        date = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
        print('from('+self.startdate+')  to(' + date + ')\n')

    def process_item(self, item, spider):
        
        if self.file == None:
            print('\nfile open ('+item['book']+')\n')
            self.file = open(item['book'], 'wb')

        print('  start writing ' + item['title'] + '...')
        text = item['title'] + '\n\n' + item['content'] + '\n\n'
        self.file.write(text.encode('utf-8'))
        return item
